package spring.rabbitmq.producer;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 用来配置队列、交换器、路由等高级信息
 *
 * @author ZCW
 * @createTime 2018/4/28
 * @lastUpdateTime 2018/4/28 ZCW
 */
@Configuration
public class RabbitMqConfig {

    public static final String HELLO = "hello";
    public static final String OBJECT = "object";
    public static final String WORKQUEUES = "workqueues";

    public static final String LOGS = "logs";

    public static final String DEAD_EXCHANGE = "dead.exchange";
    public static final String DEAD_QUEUE = "dead.queue";


    @Bean
    public List<Queue> queues() {
        List<Queue> queues = new LinkedList<>();
        queues.add(new Queue(RabbitMqConfig.HELLO));
//        queues.add(new Queue(RabbitMqConfig.OBJECT));
        queues.add(new Queue(RabbitMqConfig.WORKQUEUES));

        return queues;
    }

    @Bean
    public List<Exchange> exchanges() {
        List<Exchange> exchanges = new LinkedList<>();
        exchanges.add(new FanoutExchange(RabbitMqConfig.LOGS, true, false));
        return exchanges;
    }

    @Bean
    public Queue objectQueue() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-dead-letter-exchange", RabbitMqConfig.DEAD_EXCHANGE);
//        args.put("x-dead-letter-routing-key", "");
        return new Queue(RabbitMqConfig.OBJECT,true,false,false,args);
    }

    @Bean
    public FanoutExchange deadExchange() {
        // 死信 exchange
        return new FanoutExchange(RabbitMqConfig.DEAD_EXCHANGE, true, false);
    }

    @Bean
    public Queue deadQueue(){
        return new Queue(RabbitMqConfig.DEAD_QUEUE);
    }

    @Bean
    public Binding bindingDead(FanoutExchange deadExchange, Queue deadQueue) {
        return BindingBuilder.bind(deadQueue).to(deadExchange);
    }
}
